<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
define('FORUM_TYPE','dz');
define('SPIDER','xunku');
define('CHARSET',$_G['charset']);
define('D_P',DISCUZ_ROOT);
define('H_P',DISCUZ_ROOT.'source/plugin/xunku_editor_spider/');
define('SPIDER_LIBRARY_DIR',H_P.'library/');
define('SPIDER_REQUIRE_DIR',H_P.'require/');
error_reporting(0);
//error_reporting(E_ALL);

if(is_file(D_P.'data/plugindata/xk_editor_ownkey.php')){
	require_once(D_P.'data/plugindata/xk_editor_ownkey.php');
}
if(is_file(D_P.'data/plugindata/xk_editor_source.php')){
	require_once(D_P.'data/plugindata/xk_editor_source.php');
}
if(is_file(D_P.'data/plugindata/xk_rewrite.php')){
	require_once(D_P.'data/plugindata/xk_rewrite.php');
}
require_once(SPIDER_REQUIRE_DIR.'xk_config.php');
require_once(SPIDER_REQUIRE_DIR.'xk_common.php');
$action = getgpc('action');
$callback = getgpc('callback');
ini_set ('memory_limit',-1);
ob_end_clean();
if (CHARSET == 'gbk') {
	if (is_file(SPIDER_REQUIRE_DIR.'xk_rewrite_base.php')) {
		require_once(SPIDER_REQUIRE_DIR.'xk_rewrite_base.php');
	}	
} else {
	if (is_file(SPIDER_REQUIRE_DIR.'xk_rewrite_base_utf.php')) {
		require_once(SPIDER_REQUIRE_DIR.'xk_rewrite_base_utf.php');
	}
}

require_once (SPIDER_LIBRARY_DIR.'spider.run.php');
if ($action == 'now') {
	$range = getgpc('range');
	$subject = $message = '';
	$source_type = getSourceType($range);
	$configure = array('s_type'=>$source_type,'range'=>$range,'spider_type'=>0);
	$spider = new spider($configure);
	list($subject,$message) = $spider->t_spider->run_s();
	if (CHARSET == 'gbk') {
		$subject =  pwConvert($subject,'UTF-8','GBK');
		$message =  pwConvert($message,'UTF-8','GBK');
	}
	echo $callback.'('.json_encode(array('subject'=>$subject,'message'=>$message)).')';exit;
} elseif($action == 'later') {
	$fid = getgpc('fid');
	include template('xunku_editor_spider:add');
} else if($action == 'task') {
	$fetchUrl = getgpc('fetchUrl');
	$fetchType = getgpc('type');
	$fid = getgpc('fid');
	$tasks = array();
	foreach ($fetchUrl as $k=>$val) {
		if($fetchType[$k]) {
			$source_type = getSourceType($val);
			$configure = array(
				's_type'=>$source_type,
			);
			$spider = new spider($configure);
			$tasks = array_merge($tasks,$spider->t_spider->get_threadlist($val));
		} else {
			$tasks[] = $val;	
		}
	}
	include template('xunku_editor_spider:tasks');
} elseif($action == 'dotask') {
	$range = getgpc('fetchUrl');
	$fid = getgpc('fid');
	$posttime = getgpc('posttime');
	$replaydelay = getgpc('replaydelay');
	$source_type = getSourceType($range);
	if ($source_type) {
		$configure = array(
			's_type'=>$source_type,
			'range'=>$range,
			'spider_type'=>1,
			'fid'=>$fid,
			'post_time'=>$posttime,
			'replay_delay' =>$replaydelay,
			'post' => 0,
		);
		$spider = new spider($configure);
		$tid = $spider->t_spider->run_s();
		if(PwStrtoTime($posttime) > time()) {
			$updateTmpId = array(
				'tid' => $tid,
				'posttime' => PwStrtoTime($posttime)			
			);	
			DB::query("insert into xk_editor_tmptid set ".xkSqlSingle($updateTmpId));
		}
		C::t('forum_thread')->clear_cache($fid, 'forumdisplay_');
		if(!$tid) ajax_return(0);
		ajax_return(1,array('tid'=>$tid));
	} else {
		ajax_return(0);
	}
}
function getSourceType($url = '',$num = 0) {
	global $source_config;
	$parse_url = parse_url($url);
	$parse_url_core = explode('.',$parse_url['host']);
	$tmp_arr = array_slice($parse_url_core,$num);
	$tmp_str = implode('',$tmp_arr);
	if($num<count($parse_url_core)) {
		if(in_array($tmp_str,$source_config)) {
			return $tmp_str;
		} else {
			getSourceType($url,$num+1);
		}	
	} else {
		return false;
	}
}
?>